import com.gargoylesoftware.htmlunit.util.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created by Administrator on 2014/4/7.
 * Time: 13:29
 */
public class demohttpclient {
    /**
     * 通过httpclient 提交get方法的ajax请求
     *
     * @param url 地址
     * @return json字符串
     * @throws java.io.IOException
     */
    public String getStrByUrl(String url, String type) throws IOException {
        StringBuilder entityStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        String s1 = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpget.setConfig(requestConfig);
//        创建本地的HTTP内容
        HttpContext localContext = new BasicHttpContext();
        // 绑定定制的cookie store到本地内容中
       CookieStore cookieStore = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        CloseableHttpResponse response = httpclient.execute(httpget,localContext);
        if (type.equals("xhr")) {
            try {
                HttpEntity httpEntity = response.getEntity();
                if (httpEntity != null) {
                    try {
                        bufferedReader = new BufferedReader
                                (new InputStreamReader(httpEntity.getContent(), "GBK"), 8 * 1024);
                        String line = null;
                        while ((line = bufferedReader.readLine()) != null) {
                            entityStringBuilder.append(line + "/n");

                        }
                        // FileUtils.writeStringToFile(dest, entityStringBuilder.toString(), "GBK");
                        s1 = entityStringBuilder.subSequence(entityStringBuilder.toString().indexOf("{"), entityStringBuilder.toString().lastIndexOf("}") + 1).toString();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } finally {
                response.close();
            }
            System.out.println("解析地址：" + url);
            return s1;
        } else {
            if (type.equals("html")) {
                String Result = EntityUtils.toString(response.getEntity());
                List<org.apache.http.cookie.Cookie> lis = cookieStore.getCookies();
                for (org.apache.http.cookie.Cookie li : lis) {
                    System.out.println(li.toString());
                }
                return Result;
            }
        }
        return "";
    }

    public static void main(String[] args) {
        try {
            Long l1= System.currentTimeMillis();
            System.out.println(new demohttpclient().getStrByUrl("http://rate.taobao.com/user-rate-1763577710.htm","html"));
            Long l2= System.currentTimeMillis()-l1;
            System.out.println("处理时间:"+l2+"ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
